运行时。exec用java运行程序读取它正在做什么
我想在java中运行一个像视频游戏一样的程序,并让它输出它在java中所做的一切,这可能吗?为了澄清一切,我想看看游戏的输出,看看它在做什么。我理解它可能不是完全可以理解的,因为它将使用不同的程序语言。这里有人记得用gameshark/gamegenie破解游戏代码吗?这就是我想做的
到目前为止,我尝试了运行时和processbuilder,但没有任何结果
下面是我的代码:
public class ProcessW {
public static void main(String[] args) throws IOException {
String command = "...my game...";
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectErrorStream(true);
Process proc = pb.start();
java.io.InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
int exit = -1;
while ((line = br.readLine()) != null) {
// Outputs your process execution
System.out.println(line);
try {
exit = proc.exitValue();
if (exit == 0) {
// WinProcess finished
}
} catch (IllegalThreadStateException t) {
// The process has not yet finished.
// Should we stop it?
//if (processMustStop())
// processMustStop can return true
// after time out, for example.
proc.destroy();
}
}
}
}
# 1 楼答案
如果游戏在没有Java的情况下启动时在控制台上打印了一些东西,那么问题是缓冲。Java只有在写入4096字节的输出后才能看到游戏的输出
防止这种情况发生的唯一方法是将游戏代码更改为在每行之后调用
stdout.flush()
(或类似代码),以强制操作系统将输出发送到Java进程